package risoe.syslab.fhnew.gui;

/*
 * Copyright (c) 2012-2014, Technical University of Denmark (DTU)
 * All rights reserved.
 * 
 * The Flexhouse 2.0 platform ("this software") is licensed under the
 * BSD 3-clause license.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *   - Redistributions of source code must retain the above copyright
 *     notice, this list of conditions and the following disclaimer.
 *   - Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *   - Neither the name of DTU nor the names of its contributors may be used to
 *     endorse or promote products derived from this software without specific
 *     prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE TECHNICAL UNIVERSITY OF DENMARK BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

import java.awt.Point;

import org.apache.batik.dom.events.DOMMouseEvent;
import org.w3c.dom.events.Event;
import org.w3c.dom.events.EventListener;
import org.w3c.dom.events.EventTarget;

import risoe.syslab.fhnew.gui.VisualizationEvent.EventType;

/**
 * Class responsible for handling events related to a subscribed SVG element, i.e. a button.
 * @author M. Svendsen
 */
public class MouseEventHandler {
	private Visualization mVisualization;
	private String mId;
	private EventTarget mTarget;
	private String mVisualizationName;


	private boolean mDragging = false;

	/**
	 * Construct a new MouseEventHandler that listens to events regarding a specific SVG object
	 * @param name the name of the Visualization that holds the SVG object
	 * @param id the ID of the SVG object
	 * @param target the SVG element to listen to events from
	 * @param visualization the parent Visualization which should be notified of any events
	 */
	public MouseEventHandler(String name, String id, EventTarget target, Visualization visualization) {
		mVisualizationName = name;
		mId = id;
		mTarget = target;
		mVisualization = visualization;

		// Add mouseevent listeners
		mTarget.addEventListener("click", new OnClickAction(), false);
		mTarget.addEventListener("mousedown", new OnDownAction(), false);
		mTarget.addEventListener("mousemove", new OnMoveAction(), false);
		mTarget.addEventListener("mouseup", new OnUpAction(), false);
	}

	public class OnDownAction implements EventListener {
		public void handleEvent(Event evt) {
			mDragging = true;

			DOMMouseEvent e = (DOMMouseEvent)evt;
			Point p = new Point(e.getClientX(), e.getClientY());
			mVisualization.updatePosition(p);

			VisualizationEvent event = new VisualizationEvent(p, EventType.Mouse_Pressed, mId, mVisualizationName);
			mVisualization.issueEvent(event);
		}
	}

	public class OnUpAction implements EventListener {
		public void handleEvent(Event evt) {
			mDragging = false;

			DOMMouseEvent e = (DOMMouseEvent)evt;
			Point p = new Point(e.getClientX(), e.getClientY());
			mVisualization.updatePosition(p);

			VisualizationEvent event = new VisualizationEvent(p, EventType.Mouse_Release, mId, mVisualizationName);
			mVisualization.issueEvent(event);
		}
	}

	public class OnMoveAction implements EventListener {
		public void handleEvent(Event evt) {
			if (mDragging) {
				DOMMouseEvent e = (DOMMouseEvent)evt;
				Point p = new Point(e.getClientX(), e.getClientY());
				mVisualization.updatePosition(p);

				VisualizationEvent event = new VisualizationEvent(p, EventType.Mouse_Drag, mId, mVisualizationName);
				mVisualization.issueEvent(event);
			}
		}
	}

	public class OnClickAction implements EventListener {
		public void handleEvent(Event evt) {
			DOMMouseEvent e = (DOMMouseEvent)evt;
			Point p = new Point(e.getClientX(), e.getClientY());
			mVisualization.updatePosition(p);

			VisualizationEvent event = new VisualizationEvent(p, EventType.Mouse_Click, mId, mVisualizationName);
			mVisualization.issueEvent(event);
		}
	}
}
